<template>
  <div>
    <el-dialog v-model="visible" :show-close="false">
      <template #header="{ close, titleId, titleClass }">
        <div class="my-header">
          <h4 :id="titleId" :class="titleClass">修改订单状态</h4>
        </div>
      </template>

      <div>
        <el-form :model="formData" label-width="120px" ref="elFormRef">
          <el-form-item label="订单状态" prop="status">
            <Select :data="orderStatus" v-model="formData.status" />
          </el-form-item>
        </el-form>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="close">关闭</el-button>

          <el-button type="primary" @click="handleSubmit">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang='ts' setup>
import { ref, onMounted } from 'vue'
import { SetOrderEditStatusApi } from '@/api/index'
import { ElMessage } from 'element-plus'
import Select from '@/components/admin/Select.vue'

const props = defineProps({
  orderStatus: {
    type: Object,
    default: () => ({})
  }
})


const formData = ref({
  id: '',
  status: ''
})


const visible = ref(false)

const elFormRef = ref(null)

const open = (config) => {
  formData.value.id = config.data.id

  visible.value = true
}

const close = () => {
  visible.value = false
}


const resetFormData = () => {
  elFormRef.value.resetFields()
}

const handleSubmit = async () => {
  try {
    const res = await SetOrderEditStatusApi(formData.value)

    ElMessage({
      message: `状态修改成功!`,
      type: 'success'
    })

    resetFormData()

    emit('reload')

    close()
  } catch (err) {
    console.log('handleSubmit', err)

    ElMessage({
      message: '状态修改失败!',
      type: 'error'
    })
  }
}

// 暴露方法给父组件
defineExpose({
  open
});

const emit = defineEmits(['reload'])

onMounted(() => {
  // open()
})
</script>

<style lang='scss' scoped></style>
